package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.maof.book.dao.ForwardMapper;
import top.maof.book.model.BookInfo;
import top.maof.book.model.Forward;
import top.maof.book.model.Likes;
import top.maof.book.service.ForwardService;

@Service("forwardService")
@Transactional
public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> implements ForwardService {
    @Override
    public boolean isExist(Integer userId, Integer bookId) {
        LambdaQueryWrapper<Forward> queryWrapper = Wrappers.<Forward>lambdaQuery();
        queryWrapper.eq(Forward::getUserId, userId).eq(Forward::getBookId, bookId);
        Forward result = this.getOne(queryWrapper);
        return result != null;
    }

    @Override
    public boolean addNum(Integer userId, Integer bookId) {
        LambdaQueryWrapper<Forward> queryWrapper = Wrappers.<Forward>lambdaQuery();
        queryWrapper.eq(Forward::getUserId, userId).eq(Forward::getBookId, bookId);
        Forward result = this.getOne(queryWrapper);
        result.setNum(result.getNum() + 1);
        return this.updateById(result);
    }

//    @Override
//    public void remove(Integer userId, Integer bookId) {
//        LambdaQueryWrapper<Forward> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(Forward::getUserId, userId).eq(Forward::getBookId, bookId);
//        this.remove(queryWrapper);
//    }

    @Override
    public int forwardNum(Integer bookId) {
        return this.getBaseMapper().forwardNum(bookId);
    }
}
